﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESHOP.Domain;
using System.Data.Common;
using ESHOP.UTIL;

namespace ESHOP.DAO
{
    public class SanPhamDAO:BaseDAO
    {
        public SANPHAM Get(String ma)
        {
            return _db.SANPHAMs.Where(p => p.ID.Equals(ma)).SingleOrDefault();
        }
        public List<SANPHAM> GetList()
        {
            return _db.SANPHAMs.ToList();
        }
        public int Count()
        {
            return _db.SANPHAMs.Count();
        }
        public List<SANPHAM> GetListByCompanyID(String ma)
        {
            return _db.SANPHAMs.Where(p => p.MACONGTY.Equals(ma)&&p.ISSHOW.Equals(true)).ToList();
        }
        public List<SANPHAM> GetListFromCatrgory(String key, String loaisp)
        {
            var Query = _db.SANPHAMs.AsQueryable();
            if (!String.IsNullOrEmpty(key))
            {
                Query = Query.Where(p => p.TENSANPHAM.Contains(key)||p.GIA.ToString().Contains(key));
            }
            if (!String.IsNullOrEmpty(loaisp) && !loaisp.Equals("%"))
            {
                Query = Query.Where(p => p.MADANHMUC.Contains(loaisp));
            }
           
                Query = Query.Where(p => p.ISSHOW.Equals(true));
       
            return Query.OrderByDescending(p => p.TENSANPHAM).ToList();
        }
        public int Update(SANPHAM objui)
        {
            
            try
            {
                var objdb = Get(objui.ID);
                if (objdb != null)
                {
                    objdb.TENSANPHAM = objui.TENSANPHAM;
                    objdb.DANHMUC = objui.DANHMUC;
                    objui.HINHANH = objui.HINHANH;
                    objdb.CHITIET = objui.CHITIET;
                    objdb.GIA = objui.GIA;
                    objdb.MAQUOCGIA = objui.MAQUOCGIA;
                    objdb.MACONGTY = objui.MACONGTY;

                    _db.SubmitChanges();

                }
            }
            catch
            {
                return 0;
            }

            return 1;
        }

        public int Insert(SANPHAM objui)
        {
            DbTransaction trans = null;
            try{

                _db.Connection.Open();
                trans = _db.Connection.BeginTransaction();
                _db.Transaction = trans;
                _db.SANPHAMs.InsertOnSubmit(objui);
                _db.SubmitChanges();
                trans.Commit();

            }catch{
                if (trans != null)
                    trans.Rollback();
                return 0;
            }
            return 1;
        }

        public int Delete(String ma)
        {
            try
            {
                var objdb = Get(ma);
                if (objdb != null)
                {
                    objdb.ISSHOW = false;
                    _db.SubmitChanges();
                }
            }
            catch
            {
                return 0;
            }
            return 1;
        }

        public string NewId()
        {
            string sReturn = "";

            string sToday = "";
            sToday = DateTime.Now.Year.ToString();
            sToday = DateTime.Now.Month < 10
                         ? sToday + "0" + DateTime.Now.Month.ToString()
                         : sToday + DateTime.Now.Month.ToString();

            sToday = DateTime.Now.Day < 10
                         ? sToday + "0" + DateTime.Now.Day.ToString()
                         : sToday + DateTime.Now.Day.ToString();

            sReturn = (from p in _db.SANPHAMs.Where(p => p.ID.Substring(0, 8).Contains(sToday))
                       select p.ID).Max();

            if (!string.IsNullOrEmpty(sReturn))
            {
                int thutu = int.Parse(sReturn.Substring(8, 3));
                thutu = thutu + 1;
                if (thutu < 10)
                {
                    sReturn = sToday + "00" + thutu.ToString();
                }
                else if (thutu < 100)
                {
                    sReturn = sToday + "0" + thutu.ToString();
                }
                else if (thutu < 1000)
                {
                    sReturn = sToday + thutu.ToString();
                }
            }
            else
            {
                sReturn = sToday + "001";
            }

            return sReturn;
        }

        public List<SANPHAM> Search(String Tensp, String loaisp, String congty, String xuatxu, decimal gia, bool ishow)
        {
            var Query = _db.SANPHAMs.AsQueryable();
            if (!String.IsNullOrEmpty(Tensp))
            {
                Query = Query.Where(p => p.TENSANPHAM.Contains(Tensp));
            }
            if (!String.IsNullOrEmpty(loaisp )&& !loaisp.Equals("%"))
            {
                Query = Query.Where(p => p.MADANHMUC.Contains(loaisp));
            }
            if (!String.IsNullOrEmpty(congty)&& !congty.Equals("%"))
            {
                Query = Query.Where(p => p.MACONGTY.Contains(congty));
            }
            if (!String.IsNullOrEmpty(xuatxu) && !xuatxu.Equals("%"))
            {
                Query = Query.Where(p => p.MAQUOCGIA.Contains(xuatxu));
            }
            if (gia != 0)
            {
                Query = Query.Where(p => p.GIA == gia);
            }
            if ( ishow!= null)
            {
                Query = Query.Where(p => p.ISSHOW.Equals(ishow));
            }
            return Query.OrderByDescending(p => p.TENSANPHAM).ToList();
        }
        public List<SELL> SearchListProducts(String id, String keyword, String loaisp, String xuatxu)
        {
            var Query = _db.SELLs.ToList();
            if (!String.IsNullOrEmpty(id))
            {
                if (!String.IsNullOrEmpty(loaisp) && !loaisp.Equals("%"))
                {
                    Query = Query.Where(p => p.CATAGORY_ID.Contains(id)).ToList();
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(keyword))
                {
                    Query = Query.Where(p => p.KEYWORD.Contains(keyword) || p.DESCRIPTION.Contains(keyword) || p.SUBJECT.Contains(keyword)).ToList();
                }
                if (!String.IsNullOrEmpty(loaisp) && !loaisp.Equals("%"))
                {
                    Query = Query.Where(p => p.CATAGORY_ID.Contains(loaisp)).ToList();
                }

                if (!String.IsNullOrEmpty(xuatxu) && !xuatxu.Equals("%"))
                {
                    Query = Query.Where(p => p.MAQUOCGIA.Contains(xuatxu)).ToList();
                }
            }


            return Query.OrderByDescending(p => p.KEYWORD).ToList();
        }

        public int ActiveProduct(SANPHAM obj)
        {
            var sanpham = Get(obj.ID);
            if (sanpham != null)
            {
                sanpham.TINHTRANG = TINHTRANGSANPHAM.LA.ToString();
                _db.SubmitChanges();
                return 1;
            }
            return 0;
        }
        public int DeActiveProduct(SANPHAM obj)
        {
            var sanpham = Get(obj.ID);
            if (sanpham != null)
            {
                sanpham.TINHTRANG = TINHTRANGSANPHAM.LB.ToString();
                _db.SubmitChanges();
                return 1;
            }
            return 0;
        }

    }
}
